package com.dxj.controller;
import com.dxj.request.UserDTO;
import com.dxj.request.UserPageDTO;
import com.dxj.response.PageResultVo;
import com.dxj.response.R;
import com.dxj.response.UserInfoVo;
import com.dxj.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author dxj
 * @Description 用户接口
 * @date 2024/7/28 13:18
 */
@RestController
@Api(tags = "用户模块")
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    private IUserService userService;

    @GetMapping("/test")
    @ApiOperation(value = "测试接口")
    public String testInfo() {
        logger.info("接口入参:{}", "测试");
        return "testInfo";
    }

    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R register(@RequestBody @Validated UserDTO registerDTO) {
        logger.info("用户注册接口入参:{}", registerDTO);
        return userService.register(registerDTO);
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/userInfo/{id}")
    public R userInfo(@PathVariable(required = false) Long id) {
        logger.info("用户信息接口入参:{}", id);
        return userService.userInfo(id);
    }

    @ApiOperation(value = "用户信息修改")
    @PostMapping("/update")
    public R update(@RequestBody UserDTO userDTO) {
        logger.info("用户信息修改接口入参:{}", userDTO);
        return userService.userUpdate(userDTO);
    }

    @ApiOperation(value = "用户删除")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        logger.info("用户删除接口入参:{}", id);
        return userService.userDelete(id);
    }

    @ApiOperation(value = "用户信息列表分页")
    @GetMapping("/list")
    public R<PageResultVo<UserInfoVo>> list(@Valid UserPageDTO userPageDTO) {
        logger.info("用户信息列表接口入参:{}",userPageDTO);
        return userService.userList(userPageDTO);
    }
}
